package com.gkj.blog.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.service.IpLogService;
import com.gkj.blog.business.vo.entity.IpLogVo;
import com.gkj.blog.business.vo.entity.PvUvVo;
import com.gkj.blog.framework.object.AbstractServiceImpl;
import com.gkj.blog.persistence.mapper.IpLogMapper;
import com.gkj.blog.persistence.po.IpLog;
import com.gkj.blog.persistence.po.PvUv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.*;

@Service
public class IpLogServiceImpl extends AbstractServiceImpl<IpLogVo,IpLog> implements IpLogService{
    IpLogMapper ipLogMapper;

    @Autowired
    public void setIpLogMapper(IpLogMapper ipLogMapper) {
        this.ipLogMapper = ipLogMapper;
        setBaseMapper(ipLogMapper);
    }

    @Override
    public IpLogVo getBySidAndUri(String sid, String uri) {
        IpLog ipLog=ipLogMapper.selectBySidAndUri(sid,uri);
        if(ipLog==null){
            return null;
        }else {
            return new IpLogVo(ipLog);
        }
    }

    @Override
    public List<PvUvVo> getPvUv(Date start, Date end) {
        List<PvUvVo> pvUvVos=new LinkedList<>();
        if(end.after(start)){
            List<PvUv> uv_pvList = ipLogMapper.getUV_PVNum(start, end);
            uv_pvList.forEach((e)->{pvUvVos.add(new PvUvVo(e));});
            return pvUvVos;
        }else{
            return null;
        }
    }

    @Override
    public List<Map> getAreaUV_PV() {
        return ipLogMapper.getAreaUV_PVMap();
    }

    @Override
    public Map<String, Object> getHeapInfo() {
        MemoryUsage nonHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
        MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        Map<String,Object> map=new LinkedHashMap<String,Object>();
        map.put("nonHeap",nonHeapMemoryUsage);
        map.put("heap",heapMemoryUsage);
        return map;
    }

    @Override
    public List<Map> getSortPagePv(int size) {
        PageHelper.startPage(1,size);
        return ipLogMapper.getSortPagePv();
    }
}
